<!--
  系统监控功能组件
  创建时间：2025-09-17
  作者：开发团队
  目的：实现系统状态监控和展示功能
-->

<template>
  <div class="system-monitoring">
    <el-row :gutter="20" class="status-row">
      <el-col :span="8">
        <el-card class="status-card">
          <div class="status-header">
            <el-icon class="status-icon" color="#409EFF"><Monitor /></el-icon>
            <span class="status-title">服务器状态</span>
          </div>
          <div class="status-content">
            <div class="status-item">
              <span class="item-label">CPU使用率:</span>
              <el-progress 
                :percentage="serverStatus.cpu_usage" 
                :status="serverStatus.cpu_usage > 80 ? 'exception' : undefined"
                :stroke-width="12"
              />
              <span class="item-value">{{ serverStatus.cpu_usage }}%</span>
            </div>
            <div class="status-item">
              <span class="item-label">内存使用率:</span>
              <el-progress 
                :percentage="serverStatus.memory_usage" 
                :status="serverStatus.memory_usage > 80 ? 'exception' : undefined"
                :stroke-width="12"
              />
              <span class="item-value">{{ serverStatus.memory_usage }}%</span>
            </div>
            <div class="status-item">
              <span class="item-label">磁盘使用率:</span>
              <el-progress 
                :percentage="serverStatus.disk_usage" 
                :status="serverStatus.disk_usage > 90 ? 'exception' : undefined"
                :stroke-width="12"
              />
              <span class="item-value">{{ serverStatus.disk_usage }}%</span>
            </div>
            <div class="status-item">
              <span class="item-label">运行时间:</span>
              <span class="item-value">{{ formatUptime(serverStatus.uptime) }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card class="status-card">
          <div class="status-header">
            <el-icon class="status-icon" color="#67C23A"><DataLine /></el-icon>
            <span class="status-title">数据库状态</span>
          </div>
          <div class="status-content">
            <div class="status-item">
              <span class="item-label">连接状态:</span>
              <el-tag :type="databaseStatus.status === 'connected' ? 'success' : 'danger'">
                {{ databaseStatus.status === 'connected' ? '已连接' : '连接异常' }}
              </el-tag>
            </div>
            <div class="status-item">
              <span class="item-label">活跃连接数:</span>
              <span class="item-value">{{ databaseStatus.connections }}</span>
            </div>
            <div class="status-item">
              <span class="item-label">数据库大小:</span>
              <span class="item-value">{{ formatFileSize(databaseStatus.size) }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card class="status-card">
          <div class="status-header">
            <el-icon class="status-icon" color="#E6A23C"><Setting /></el-icon>
            <span class="status-title">服务状态</span>
          </div>
          <div class="status-content">
            <el-table :data="servicesStatus" style="width: 100%" size="small">
              <el-table-column prop="name" label="服务名称" />
              <el-table-column prop="status" label="状态" width="80">
                <template #default="scope">
                  <el-tag :type="scope.row.status === 'running' ? 'success' : 'danger'">
                    {{ scope.row.status === 'running' ? '运行中' : '异常' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="last_check" label="最后检查" width="120">
                <template #default="scope">
                  {{ formatDate(scope.row.last_check) }}
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-card class="chart-card">
      <template #header>
        <div class="card-header">
          <span>系统资源使用趋势</span>
          <el-button type="primary" @click="fetchSystemStatus" :icon="Refresh">刷新</el-button>
        </div>
      </template>
      
      <div class="chart-container">
        <!-- TODO: 实现图表展示 -->
        <div class="chart-placeholder">
          <el-icon size="48"><PieChart /></el-icon>
          <p>图表展示功能待实现</p>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
import { Refresh, Monitor, DataLine, Setting, PieChart } from '@element-plus/icons-vue'
import { superAdminApi } from '@/api/superadmin'
import { showError, showSuccess } from '@/utils/message'
import type { SystemStatusData } from '@/api/superadmin'
import { formatDate, formatFileSize } from '@/utils/format'

// 状态数据
const serverStatus = ref({
  cpu_usage: 0,
  memory_usage: 0,
  disk_usage: 0,
  uptime: 0
})

const databaseStatus = ref({
  status: 'unknown',
  connections: 0,
  size: 0
})

const servicesStatus = ref<Array<{
  name: string
  status: string
  last_check: string
}>>([])

// 加载状态
const loading = ref(false)

/**
 * 格式化运行时间
 * @param seconds 运行秒数
 * @returns 格式化的时间字符串
 */
const formatUptime = (seconds: number): string => {
  const days = Math.floor(seconds / 86400)
  const hours = Math.floor((seconds % 86400) / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  
  if (days > 0) {
    return `${days}天${hours}小时${minutes}分钟`
  } else if (hours > 0) {
    return `${hours}小时${minutes}分钟`
  } else {
    return `${minutes}分钟`
  }
}

/**
 * 获取系统状态
 */
const fetchSystemStatus = async () => {
  try {
    loading.value = true
    
    const response = await superAdminApi.getSystemStatus()
    
    // 由于响应拦截器已经处理了响应格式，这里直接获取数据
    // response 是 { server, database, services } 结构
    
    // 更新服务器状态
    serverStatus.value = {
      cpu_usage: response.server?.cpu_usage || 0,
      memory_usage: response.server?.memory_usage || 0,
      disk_usage: response.server?.disk_usage || 0,
      uptime: response.server?.uptime || 0
    }
    
    // 更新数据库状态
    databaseStatus.value = {
      status: response.database?.status || 'unknown',
      connections: response.database?.connections || 0,
      size: response.database?.size || 0
    }
    
    // 更新服务状态
    servicesStatus.value = response.services || []
  } catch (error) {
    console.error('获取系统状态失败:', error)
    showError('获取系统状态失败')
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取系统状态
onMounted(() => {
  fetchSystemStatus()
})
</script>

<style scoped>
.system-monitoring {
  padding: 20px;
}

.status-row {
  margin-bottom: 20px;
}

.status-card {
  height: 100%;
}

.status-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.status-icon {
  font-size: 24px;
  margin-right: 10px;
}

.status-title {
  font-size: 18px;
  font-weight: bold;
}

.status-content {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.status-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.item-label {
  font-weight: 500;
}

.item-value {
  font-weight: bold;
  color: #303133;
}

.chart-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-placeholder {
  text-align: center;
  color: #909399;
}

.chart-placeholder p {
  margin-top: 10px;
}
</style>